package com.ciwong.mobilelib.utils.animtoast;

import android.content.Context;
import android.widget.Toast;

import java.util.LinkedList;

public class ToastAnimDecorate {
    public static final long LENGTH_3000 = ToastAnim.LENGTH_3000;
    public static final long LENGTH_2000 = ToastAnim.LENGTH_2000;
    private static LinkedList<BaseToastAnim> listToasts = new LinkedList<>();
    boolean flagFirst = true;
    private static ToastAnimDecorate IINSTANCE;
    private IEndAllAnimToast iEndAllAnimToast;

    public static ToastAnimDecorate getInstance() {
        if (IINSTANCE == null) {
            IINSTANCE = new ToastAnimDecorate();
        }
        return IINSTANCE;
    }

    /**
     * @param context
     * @param content
     * @param duration
     * @return
     */
    public ToastAnimDecorate makeText(Context context, CharSequence content, long duration) {
//        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        addNativeToasts(context, content, duration);
//        } else{
//            addAnimToasts(context, content, duration);
//        }
        return IINSTANCE;
    }

    /**
     * @param context
     * @param content
     * @param duration
     * @return
     */

    public ToastAnimDecorate makeText(Context context, String content, long duration) {
//        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        addNativeToasts(context, content, duration);
//        } else{
//            addAnimToasts(context, content, duration);
//        }
        return IINSTANCE;
    }

    /**
     * 动画toast
     *
     * @param context
     * @param strId
     * @param duration
     * @return
     */
    public ToastAnimDecorate makeText(Context context, int strId, int duration) {
//        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        addNativeToasts(context, context.getText(strId), duration);
//        } else{
//            addAnimToasts(context, context.getText(strId), duration);
//        }
        return IINSTANCE;
    }

    private ToastAnimDecorate addAnimToasts(Context context, CharSequence content, long duration) {
        ToastAnim animToasts = makeAnimToasts(context, content, duration);
        if (listToasts != null)
            listToasts.add(animToasts);
        return IINSTANCE;
    }

    private ToastAnim makeAnimToasts(Context context, CharSequence content, long duration) {
        ToastAnim helper = new ToastAnim(context);
        helper.setDuration(duration);
        helper.setContent(content);
        return helper;
    }

    /**
     * 原生toast
     *
     * @param context
     * @param content
     * @param duration
     * @return
     */
    private ToastAnimDecorate addNativeToasts(Context context, CharSequence content, long duration) {
        int durationToast = LENGTH_2000 == duration ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
        ToastNative mToast = makeNativeToasts(context, content, durationToast);
        if (listToasts != null)
            listToasts.add(mToast);
        return IINSTANCE;
    }

    private ToastNative makeNativeToasts(Context context, CharSequence content, int duration) {
        ToastNative mToast = new ToastNative(context);
        mToast.setDuration(duration);
        mToast.setmToastContent(content);
        return mToast;
    }

    public void show() {
        System.out.println("listToasts.size:" + listToasts.size());
        if (listToasts != null && listToasts.size() > 0) {
            if (flagFirst) {
                flagFirst = false;
                BaseToastAnim firstToast = listToasts.removeFirst();
                showView(firstToast);
            }
        }
    }

    private void showView(BaseToastAnim firstToast) {
        firstToast.show();
        firstToast.setOnRemoveViewListener(new BaseToastAnim.IRemoveViewListener() {
            @Override
            public void viewRemoved() {
                if (listToasts != null && listToasts.size() > 0) {
                    showView(listToasts.removeFirst());
                } else {
                    if (iEndAllAnimToast != null)
                        iEndAllAnimToast.endAllAnimToast();
                    flagFirst = true;
                }
            }
        });
    }


    public LinkedList getToastList() {
        return listToasts;
    }

    public interface IEndAllAnimToast {
        void endAllAnimToast();
    }

    public void setIEndAllAnimToast(IEndAllAnimToast iEndAllAnimToast) {
        this.iEndAllAnimToast = iEndAllAnimToast;
    }
}
